﻿/*
 * FileName: MatchRiskFactor.cs
 * Module: Core
 * Author: Gourav Das
 * Created: 05 June 2011
 * Modifications:
 * Copyright: Gourav Das, 2011
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _22yards.Core.MatchPlayEngine
{
    public class MatchRiskFactor
    {
        const double DASCONSTANT = 6.1;
        const double DASCONSTANT2 = 7.5;

        public static double GetMatchRiskFactor(int _runs, int _balls, short _wktsRemaining, short _type)
        {
            double MRF = 0.0;
            double RPB = (double)_runs / (double)_balls;

            if (_type == 1)
            {
                if (_balls <= 0 || _wktsRemaining <= 0)
                    return 0.0;
                if ((RPB > 6.1))
                    return 20.0;
                if (_runs > 100 * Math.Sqrt((double)_wktsRemaining))
                    return 50.0;

                MRF = (RPB + (Math.Sqrt((double)_balls) / 10) + Math.Sqrt((double)_wktsRemaining) / 10);
                if (_balls < 121)
                    MRF = (MRF * 100) / DASCONSTANT;
                if (_balls >= 121 && _balls < 301)
                    MRF = (MRF * 100) / DASCONSTANT2;
            }
            if (MRF > 100)
                return 100.0;
            return MRF;
        }
    }
}
